package com.zr.d_c.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.d_c.model.Customer;
import com.zr.d_c.model.User;
import com.zr.d_c.service.BuyCartService;
import com.zr.d_c.service.CustomerService;
import com.zr.d_c.serviceimpl.BuyCartServiceImpl;
import com.zr.d_c.serviceimpl.CustomerServiceImpl;
/**
 * 展示购物车中的菜品信息
 * @author Administrator
 *
 */
public class ShowByCartController extends HttpServlet {

	CustomerService customerservice = new CustomerServiceImpl();
	BuyCartService buycartservice = new BuyCartServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = req.getSession();
		PrintWriter pw = resp.getWriter();
		User u = (User) session.getAttribute("currentUser");
		int uid = u.getUid();
		Customer c = customerservice.getCustomerByUid(uid);
		int cid = c.getCid();
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		int start = (page - 1)*pageSize;
		String searchsname = req.getParameter("searchsname");
		if(searchsname==null){
			searchsname = "";
		}
		if(searchsname.equals("")){
			pw.write(buycartservice.getBuyCartByCid(cid, start, pageSize).toString());
		}else{	
			pw.write(buycartservice.getBuyCartByCidAndSname(cid, start, pageSize, searchsname).toString());
		}
		pw.close();
	}
}
